Add unicode math shorthand for $...$ regions in labels#89
Merged
Conversation
19 tasks
Any label (title, axis labels, TextPlot markdown bodies) may embed
$...$ math written in LaTeX-ish syntax. Math regions are lowered to
inline Unicode by every backend, including the terminal:
$\sigma^2$ -> σ²
$\frac{a+b}{c}$ -> (a+b)/c
$\sqrt{x^2+y^2}$ -> √(x²+y²)
$\sum_{i=1}^{n} x_i$ -> ∑ᵢ₌₁ⁿ xᵢ
Zero dependencies, no feature flag, deliberately inline-only (no 2-D
layout). Super/subscript substitution is all-or-nothing: if any char in
the group lacks a Unicode form the whole group falls back to a clean
x^(2q), never a half-substituted mix. The lowering never emits a stray
backslash or dollar; literal dollars are written \$.
- src/render/math.rs: detection, command table, to_unicode + 21 unit tests
- backends: SVG, raster (PNG/PDF), terminal Text arms lower math inline
- render: markdown TextSpans lowered after markup parsing
- tests/math_lookup.rs: per-backend integration tests
- smoke_tests.sh: 7 checks across plot types and label slots
- terminal_plots.sh: math labels entry
- docs: Reference -> Math in Labels + generated example SVGs
bcd562f to
d9e8790
Compare
Owner
|
This is great. Thanks |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This is the unicode shorthand half of #88, split out per the review discussion there — the typst math layer and typst backend stay parked on the other branch for later.
Any label (title, axis labels,
TextPlotmarkdown bodies) may embed$...$math written in LaTeX-ish syntax. Math regions are lowered to inline Unicode by every backend, including the terminal:$\sigma^2$$x_i$$a \leq b \cdot c$$\frac{a+b}{c}$$\sqrt{x^2+y^2}$$\sum_{i=1}^{n} x_i$Zero dependencies, no feature flag, always on. No
Cargo.tomlchanges at all.Design notes
a/band√(…), never stacked 2-D layout — the output is plain text that flows anywhere a label can go (rotated y-axis titles, terminal character grids, markdown bodies).x^{2n}→ x²ⁿ, but if any character in the group lacks a Unicode form (q, most capitals), the whole group falls back to a cleanx^(2q)— never a half-substituted mix.\or$. Literal dollars are written\$; an unclosed$is left untouched as ordinary text.src/render/math.rsholds detection (contains_math), segmentation, the command table, andto_unicode. The SVG, raster (PNG/PDF), and terminalTextarms lower at draw time; markdownTextSpans are lowered once after markup parsing, so**bold** $\sigma^2$works inTextPlotbodies.Supported syntax
Greek letters (incl.
\varepsilon/\varphivariants), operators/relations/arrows (\pm \times \cdot \leq \neq \approx \in \partial \nabla \infty \to \degree…),\frac{a}{b},\sqrt{x}/\sqrt[3]{x}(→ ³√x), and^/_scripts with{...}groups or braceless\commandoperands (x^\alpha→x^(α)).Testing
render::math— substitution, fallbacks, escaped/unclosed dollars, nested chains (quadratic formula end-to-end), no-leak guarantees.tests/math_lookup.rs— per-backend integration: terminal, SVG, markdownTextPlotbody, escaped-dollar literal.scripts/terminal_plots.sh— math labels entry for visual terminal inspection.cargo ci-test(99 suites),cargo ci-clippy, and the full smoke suite (190 checks) all pass.Docs
examples/math.rs, committed underdocs/src/assets/math/).lib.rs; CHANGELOG entry.🤖 Generated with Claude Code